const net = require('net');
const port = 9000;
const host = '127.0.0.1';

var client = new net.Socket();
client.setEncoding = 'UTF-8';

//连接服务器
client.connect(port,host,()=>{
    client.write('你好');
});

client.on('data',(data)=>{
    console.log('服务端传来： '+data);
    say();
});

client.on('error',(error)=>{
    console.log('error '+error);
});

client.on('close',(error)=>{
   console.log('connection closed');
});

const readline = require('readline');
const process = require('process');

const rl = readline.createInterface({
    input :process.stdin,
    output :process.stdout
});


var say = function () {
    rl.question('请输入：',(inputStr)=>{
        if(inputStr != 'bye'){
            client.write(inputStr+'\n');
        }else {
            client.distroy();//关闭连接
            rl.close();
        }
    })
}